//
//  SXNetworkManager.m
//  iOS_Router_Runtime
//
//  Created by 王水鑫 on 2018/12/13.
//  Copyright © 2018年 王水鑫. All rights reserved.
//

#import "SXNetworkManager.h"

#define PAMailHostAddress @"https://web.yanzhi.work/MomailService" //新接口
@interface SXNetworkManager ()

@property (weak, nonatomic) id<SXNetworkManagerDelegate> delegate;

@end

@implementation SXNetworkManager
// 超时处理 - 模拟
- (void)SXTimeoutHandler {
    [self performSelector:@selector(SXExecuteErrorPromptInsteadOfLoadingAnimation) withObject:nil afterDelay:10];
}
- (void)request:(void(^)(NSDictionary *response))success {
    NSLog(@"前%@", [NSDate date]);
    [self SXTimeoutHandler];
    // 模拟网络请求步骤
    [self performSelector:@selector(response) withObject:nil afterDelay:25];
    NSDictionary *reponse = @{@"State":@"成功！", @"text":@(1546329759)};
    success(reponse);
}
- (void)response {
    NSLog(@"后%@", [NSDate date]);
    [self SXNetworkRunningState:NO];
    NSDictionary *response = @{@"key":@"value"};
//    [self SXNetworkSuccessReturnJsonData:response urlPath:nil parameters:nil];
}
-(void)task:(NSProgress *)progress {
    //完成任务单元数+1
    if (progress.completedUnitCount < progress.totalUnitCount) {
        progress.completedUnitCount += 1;
    }
}
// ===== 模拟上述 ======

#pragma mark - ==================== public method ====================
// Post请求服务器
+ (void)launchPostRequestToRemoteServerWithURLPath:(NSString *)urlPath parameters:(NSDictionary *)parameters delegate:(id<SXNetworkManagerDelegate>)delegate managerObject:(SXNetworkManager *)managerObject {
    managerObject.delegate = delegate;
    NSString *URL = [NSString stringWithFormat:@"%@%@",PAMailHostAddress, urlPath];
    [managerObject SXNetworkRunningState:YES];
    NSLog(@"launch:%@", managerObject.class);
    
    [managerObject request:^(NSDictionary *response) {
        [managerObject SXNetworkRunningState:NO];
        [managerObject SXNetworkSuccessReturnJsonData:response urlPath:urlPath parameters:parameters];
    }];
}

#pragma mark - ==================== private method ====================
// 监听网络请求状态
- (void)SXNetworkRunningState:(BOOL)isRunning {
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerRunning:)]) {
        [self.delegate SXNetworkManagerRunning:isRunning];
    }
}

// 服务器返回数据 - 远程本地获取到数据时，统一调用这个方法
- (void)SXNetworkSuccessReturnJsonData:(NSDictionary *)response urlPath:(NSString *)urlPath parameters:(NSDictionary *)parameters {
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerSuccessReturnJsonResponse:urlPath:parameters:)]) {
        [self.delegate SXNetworkManagerSuccessReturnJsonResponse:response urlPath:urlPath parameters:parameters];
    }
}

// 与服务器交互进程/进度，单个任务的上传下载任务进度
- (void)SXNetworkProgress:(NSProgress *)progress {
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerLoadingProgress:)]) {
        [self.delegate SXNetworkManagerLoadingProgress:progress.fractionCompleted];
    }
}

// 多个任务单元完成进度
- (void)SXNetworkFinishedTaskQuantity:(int)quantity {
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerFinishedTaskQuantity:)]) {
        [self.delegate SXNetworkManagerFinishedTaskQuantity:quantity];
    }
}

// 停止加载动画，提示错误信息
- (void)SXExecuteErrorPromptInsteadOfLoadingAnimation {
    if (!self.errorPrompt) {
        self.errorPrompt = @"请求失败";
    }
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerExecuteErrorPromptInsteadOfLoadingAnimation:)]) {
        [self.delegate SXNetworkManagerExecuteErrorPromptInsteadOfLoadingAnimation:self.errorPrompt];
    }
}

// 请求失败时，返回error信息
- (void)SXReturnErrorInfo:(NSError *)error {
    if ([self.delegate respondsToSelector:@selector(SXNetworkManagerFailureReturnErrorInfo:)]) {
        [self.delegate SXNetworkManagerFailureReturnErrorInfo:error];
    }
}

@end
